#!/usr/bin/python
# coding:utf8
# Author=张其川
# 官方QQ群=2418731289
VERSION = 131
import socket, os, sys, db_to_redis_allconf, cheungssh_su, cheungssh_sudo

socket.setdefaulttimeout(3)
import paramiko, threading, time, commands, re, Format_Char_Show_web, shutil, random, json, sendinfo, key_resolv
from models import ServerConf
from django.core.cache import cache
import json
from redis_to_redis import set_redis_data


def ip_to_id(Data):
    Data.FailID = []
    for ip in Data.FailIP:
        for id in Data.conf.keys():
            if Data.conf[id]['ip'] == ip:
                Data.FailID.append(id)


def SSH_cmd(ip, username, password, port, loginmethod, keyfile, cmd, ie_key, group, Data, tid):
    PROFILE = ". /etc/profile 2&>/dev/null;. ~/.bash_profile 2&>/dev/null;. /etc/bashrc 2&>/dev/null;. ~/.bashrc 2&>/dev/null;"
    PATH = "export PATH=$PATH:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin;"
    start_time = time.time()
    ResultSum = ''
    ResultSumLog = ''
    color_status = 0
    try:
        o = None
        err = None
        ssh = paramiko.SSHClient()
        if loginmethod == 'KEY':

            KeyPath = keyfile
            key = paramiko.RSAKey.from_private_key_file(KeyPath)
            ssh.load_system_host_keys()
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            ssh.connect(ip, port, username, pkey=key)
        else:
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            ssh.connect(ip, int(port), username, password)
        stdin, stdout, stderr = ssh.exec_command(PROFILE + PATH + cmd)
        out = stdout.readlines()
        Data.All_Servers_num += 1
        for o in out:
            ResultSum += o
            ResultSumLog += o.strip('\n') + '\\n'

        error_out = stderr.readlines()
        for err in error_out:
            ResultSum += err
            ResultSumLog += err.strip('\n') + '\\n'
        if err:
            Data.FailIP.append(ip)
            color_status = 1
            ResultSum_count = "服务器: %s@%s  错误消息: %s" % (username, ip, "命令失败")
            out = 'Null\n'
        else:
            error_out = 'NULL'
            ResultSum_count = "服务器: %s@%s " % (username, ip)
            Data.All_Servers_num_Succ += 1
        Show_Result_web_status = Format_Char_Show_web.Show_Char(ResultSum.replace("<", "&lt;") + ip, color_status)
        Show_Result = ResultSum + '\n' + ResultSum_count
        jindu = int(float(Data.All_Servers_num) / float(Data.All_Servers_num_all) * 100)
        TmpShow = Format_Char_Show_web.Show_Char(Show_Result + " 命令: " + cmd, color_status)
        if color_status == 1:
            info = {"msgtype": 1, "content": [{"group": group, "servers": [
                {"ip": ip, "status": "ERR", "jindu": jindu, "cmd": cmd, "info": Show_Result_web_status}]}]}
        else:
            info = {"msgtype": 1, "content": [{"group": group, "servers": [
                {"ip": ip, "status": "OK", "jindu": jindu, "cmd": cmd, "info": Show_Result_web_status}]}]}
        b_id = str(random.randint(999999999, 99999999999999999))
        info["id"] = b_id
        info = json.dumps(info, encoding='utf8', ensure_ascii=False)
    except Exception, e:
        print e
        ResultSum = str(e)
        color_status = 1
        Data.FailIP.append(ip)
        Data.All_Servers_num += 1
        ResultSum_count = "服务器: %s@%s  错误消息: %s" % (username, ip, e)
        Show_Result_web_status = ResultSum
        Show_Result = ResultSum + ResultSum_count

        TmpShow = Format_Char_Show_web.Show_Char(Show_Result + " 命令: " + cmd, color_status)
        jindu = int(float(Data.All_Servers_num) / float(Data.All_Servers_num_all) * 100)
        Show_Result_web_status = Format_Char_Show_web.Show_Char(str(e).replace("<", "&lt;") + "\n" + ip,
                                                                    color_status)
        info = {"msgtype": 1, "content": [{"group": group, "servers": [
            {"ip": ip, "status": "ERR", "jindu": jindu, "cmd": cmd, "info": Show_Result_web_status}]}]}
        info['id'] = (str(random.randint(999999999, 99999999999999999)))
        info = json.dumps(info, encoding='utf8', ensure_ascii=False)
    finally:
        ssh.close()
    if Data.excutetype == 'cmd':
        print '命令方式'
        sendinfo.sendinfo(str({ie_key: info}))
    elif Data.excutetype == 'crontab':
        print '计划任务'
        crondlog_show = cache.get('crondlog')
        if crondlog_show:
            lasttime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
            crondlog_show[Data.fid]['content'] = ResultSum
            crondlog_show[Data.fid]['lasttime'] = lasttime
            if color_status == 0:
                crondlog_show[Data.fid]['status'] = "正常"
            else:
                crondlog_show[Data.fid]['status'] = "失败"
            cache.set('crondlog', crondlog_show, 8640000000)

        else:

            pass
    else:
        checktime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        ipinfo = {Data.hwtype: ResultSum, 'ip': ip, 'checktime': checktime}
        hwinfo = cache.get('hwinfo')
        if not hwinfo:
            hwinfo = {ip: ipinfo}
        else:
            if not hwinfo.has_key(ip):
                hwinfo[ip] = ipinfo
        hwinfo[ip][Data.hwtype] = ResultSum
        hwinfo[ip]['checktime'] = checktime
        cache.set('hwinfo', hwinfo, 864000000)
    R = set_redis_data('cmd.%s.%s' % (tid, ip), json.dumps(ResultSum, encoding="utf-8", ensure_ascii=False))
    print "cmd.%s.%s" % (tid, ip)


def main(cmd, ie_key, selectserver, Data, tid, excutetype='cmd', hwtype='CPU'):
    Data.excutetype = excutetype
    Data.hwtype = hwtype
    Data.FailIP = []
    Data.All_Servers_num_all = 0
    Data.All_Servers_num = 0
    Data.All_Servers_num_Succ = 0
    Data.i = 0
    all_conf = db_to_redis_allconf.allhostconf()['content']
    Data.conf = all_conf
    if selectserver == 'all':
        selectserver = Data.conf.keys()
    else:
        selectserver = selectserver.split(',')
    print selectserver
    Data.All_Servers_num_all = len(selectserver)
    Data.i = len(selectserver)
    for id in selectserver:
        if Data.conf[id]['loginmethod'] == 'KEY':
            keyfile = key_resolv.key_resolv(Data.conf[id]["keyfile"], cache)
        else:
            keyfile = 'N'
        if Data.conf[id]["su"] == "Y" and excutetype == 'cmd':
            b = threading.Thread(target=cheungssh_su.Excute_suroot, args=(
                Data.conf[id]["ip"], Data.conf[id]["username"], Data.conf[id]["password"], Data.conf[id]["port"],
                Data.conf[id]["loginmethod"], keyfile, cmd, ie_key, Data.conf[id]["group"], Data.conf[id]["supassword"],
                Data, tid))
        elif Data.conf[id]["sudo"] == "Y" and excutetype == 'cmd':
            b = threading.Thread(target=cheungssh_sudo.Excute_sudo, args=(
                Data.conf[id]["ip"], Data.conf[id]["username"], Data.conf[id]["password"], Data.conf[id]["port"],
                Data.conf[id]["loginmethod"], keyfile, cmd, ie_key, Data.conf[id]["group"],
                Data.conf[id]["sudopassword"],
                Data, tid))
        else:
            b = threading.Thread(target=SSH_cmd, args=(
                Data.conf[id]["ip"], Data.conf[id]["username"], Data.conf[id]["password"], Data.conf[id]["port"],
                Data.conf[id]["loginmethod"], keyfile, cmd, ie_key, Data.conf[id]["group"], Data, tid))
        b.start()
    b.join()
    ip_to_id(Data)
    cache.set(ie_key, Data.FailID, 864000)


if __name__ == '__main__':
    from DataConf import DataConf

    Data = DataConf()
    fid = sys.argv[1]
    Data.fid = fid
    cmdfile = "/home/cheungssh/data/cmd/cmdfile"
    fcontent = open(cmdfile)
    cmd = ''
    selectserver = ''
    for a in fcontent:
        cmdline = a.strip().split('#')
        if cmdline[-1] == fid:
            cmd = "#".join(cmdline[0:-2])
            selectserver = cmdline[-2]
            fcontent.close()
            break
    print cmd
    fcontent.close()
    main(cmd, 'all-ie', selectserver, Data, fid, 'crontab')
